

// background.ts
import { API_URL } from './constant';

interface HtmlResponse {
    code: number;
    data: string[];
}

interface Html {
    getHtmlNew(url: string, options: { try_times: number }, callback: (response: string) => void): void;
    postHtmlNew(url: string, options: { data: { url: string }; httpType: string }, callback: (response: HtmlResponse) => void): void;
}

interface URLManager {
    url: {
        postAmazon: () => string;
    }
}

// Define Html and s objects or fetch them from somewhere
const Html: Html = {
    getHtmlNew: (url, options, callback) => {
        // Example implementation using fetch
       
        fetch(url)
            .then(response => response.text())
            .then(text => callback(text))
            .catch(error => console.error('Error fetching HTML:', error));
    },
    postHtmlNew: (url, options, callback) => {
        // Example implementation using fetch
        fetch(url, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            body: new URLSearchParams(options.data as any).toString(),
        })
            .then(response => response.json())
            .then(data => callback(data))
            .catch(error => console.error('Error posting HTML:', error));
    }
};


chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
    if (request.type === 'callApi') {
      fetch(`${API_URL}/api/goods-collect/commonData/insert/plusCollectGoods`, {
        method: 'POST',
        headers: { 
            'Content-Type': 'application/json' ,
            'Authorization':"Bearer " +request.token
    },
        body: JSON.stringify({
            ...request.data,
            version: chrome.runtime.getManifest().version,
            collectVersion: 0,
        })
      })
      .then(res => res.json())
      .then(data => sendResponse(data))
      .catch(err => sendResponse({ error: err.message }));
      return true; // 保持消息通道开放
    }
  });

// Listen for messages from content scripts
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
    if (request.action === 'scrape') {
        
        const url = request.url;
        Html.getHtmlNew(url, { try_times: 0 }, (response) => {
           
            // Process response here
            sendResponse({ data: response });
        });
        return true; // Indicates you want to send a response asynchronously
    }
});

  // //Amazon
  //'amazon.': {
    //detect: function (url) {
      //  if (url.indexOf('/dp/') !== -1 || url.indexOf('/gp/product/') !== -1) {
        //    return 'detail';
        //}
        //if (url.indexOf('amazon.com/s?') !== -1
          //  || url.indexOf('/stores/page/') !== -1) {
           // return 'category';
        //}
    //},
    //detail: function (url) {
      //  if (url.indexOf('/dp/') !== -1) {
        //    return true;
        //}
        //if (url.indexOf('amazon.com/gp/product/') !== -1
          //  || url.indexOf('/gp/slredirect/picassoRedirect.html/') !== -1) {
           // return true;
       // }
    //}
//},